<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/
	
require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestDbAccessor extends PHPUnit_Framework_TestCase {

	function test_db_query_select_without_where() {
		$expected_vars = array('foo', 'bar');	
		$expected_tables = array('USER', 'PERSONEL');

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_select($expected_vars, $expected_tables, '');
		$this->assertEquals("SELECT\n\tbar, \n\tfoo, 0\nFROM\n\tPERSONEL, USER", $actual);
	}

	function test_db_query_select_with_array_var() {
		$expected_vars = array(array('foo', 'oof'), 'bar');	
		$expected_tables = array('USER', 'PERSONEL');

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_select($expected_vars, $expected_tables, '');
		$this->assertEquals("SELECT\n\tbar, \n\tUSER.oof, USER.foo, 0\nFROM\n\tPERSONEL, USER", $actual);
	}

	function test_db_query_select_with_where() {
		$expected_vars = array('foo', 'bar');	
		$expected_tables = array('USER', 'PERSONEL');
		$expected_where = "USER.foor = 'has_to_be_true'";

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_select($expected_vars, $expected_tables, $expected_where);
		$this->assertEquals("SELECT\n\tbar, \n\tfoo, 0\nFROM\n\tPERSONEL, USER\n" .
				"WHERE\n\t$expected_where", $actual);
	}

	function test_db_query_select_with_where_and_order() {
		$expected_vars = array('foo', 'bar');	
		$expected_tables = array('USER', 'PERSONEL');
		$expected_where = "USER.foor = 'has_to_be_true'";
		$expected_order = 'DESC';

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_select($expected_vars, $expected_tables, 
				$expected_where, $expected_order);
		$this->assertEquals("SELECT\n\tbar, \n\tfoo, 0\nFROM\n\tPERSONEL, USER\n" .
				"WHERE\n\t$expected_where\nORDER BY\n\t$expected_order", $actual);
	}

	function test_db_query_with_where_and_order_and_limit() {
		$expected_vars = array('foo', 'bar');	
		$expected_tables = array('USER', 'PERSONEL');
		$expected_where = "USER.foor = 'has_to_be_true'";
		$expected_order = 'DESC';
		$expected_limit = '0, 10';

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_select($expected_vars, $expected_tables, 
				$expected_where, $expected_order, $expected_limit);
		$this->assertEquals("SELECT\n\tbar, \n\tfoo, 0\nFROM\n\tPERSONEL, USER\n" .
				"WHERE\n\t$expected_where\nORDER BY\n\t$expected_order\n" . 
				"LIMIT $expected_limit", $actual);
	}

	function test_db_query_with_where_and_order_and_limit_and_group() {
		$expected_vars = array('foo', 'bar');	
		$expected_tables = array('USER', 'PERSONEL');
		$expected_where = "USER.foor = 'has_to_be_true'";
		$expected_order = 'DESC';
		$expected_limit = '0, 10';
		$expected_group = 'USER.foo';

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_select($expected_vars, $expected_tables, 
				$expected_where, $expected_order, $expected_limit, $expected_group);
		$this->assertEquals("SELECT\n\tbar, \n\tfoo, 0\nFROM\n\tPERSONEL, USER\n" .
				"WHERE\n\t$expected_where\nGROUP BY\n\t$expected_group\nORDER BY\n\t$expected_order\n" . 
				"LIMIT $expected_limit", $actual);
	}

	function test_db_query_with_where_and_order_and_limit_and_group_and_having() {
		$expected_vars = array('foo', 'bar');	
		$expected_tables = array('USER', 'PERSONEL');
		$expected_where = "USER.foor = 'has_to_be_true'";
		$expected_order = 'DESC';
		$expected_limit = '0, 10';
		$expected_group = 'USER.foo';
		$expected_having = 'presents';

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_select($expected_vars, $expected_tables, 
				$expected_where, $expected_order, $expected_limit, $expected_group, 
				$expected_having);
		$this->assertEquals("SELECT\n\tbar, \n\tfoo, 0\nFROM\n\tPERSONEL, USER\n" .
				"WHERE\n\t$expected_where\nGROUP BY\n\t$expected_group\nHAVING\n\t" .
				"$expected_group\nORDER BY\n\t$expected_order\n" . 
				"LIMIT $expected_limit", $actual);
	}

	//TODO: this is just plain incorrect
	function test_db_query_with_where_and_order_and_limit_and_having_but_no_group() {
		$expected_vars = array('foo', 'bar');	
		$expected_tables = array('USER', 'PERSONEL');
		$expected_where = "USER.foor = 'has_to_be_true'";
		$expected_order = 'DESC';
		$expected_limit = '0, 10';
		$expected_group = '';
		$expected_having = 'presents';

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_select($expected_vars, $expected_tables, 
				$expected_where, $expected_order, $expected_limit, $expected_group, $expected_having);
		$this->assertEquals("SELECT\n\tbar, \n\tfoo, 0\nFROM\n\tPERSONEL, USER\n" .
				"WHERE\n\t$expected_where\nHAVING\n\t" .
				"$expected_group\nORDER BY\n\t$expected_order\n" . 
				"LIMIT $expected_limit", $actual);
	}

	function test_db_query_insert() {
		$expected_keys = array('foo', 'bar');
		$expected_values = array('Zaphod', 'Beeblebrox');
		$table = 'CREW';

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_insert($expected_keys, $expected_values, $table);
		$this->assertEquals("INSERT INTO CREW\n\t(bar, foo) VALUES\n\t(Beeblebrox, Zaphod)", $actual);
	}

	function test_db_query_insert_multiple() {
		$expected_keys = array('firstname', 'lastname');
		$expected_values = array(array('Zaphod', 'Beeblebrox'),
				array('Arthur', 'Dent'),
				array('Tricia', 'McMillan'),
				array('Marvin', 'The Paranoid Android'));
		$table = 'CREW';

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_insert_multiple($expected_keys, $expected_values, $table);
		$this->assertEquals("INSERT INTO CREW\n\t(lastname, firstname) VALUES\n\t" .
				"(The Paranoid Android, Marvin),(McMillan, Tricia),(Dent, Arthur),(Beeblebrox, Zaphod)", 
				$actual);
	}

	function test_db_query_update() {
		$expected_keys = array('firstname', 'lastname');
		$expected_values = array('Zaphod', 'Beeblebrox');
		$expected_table = 'CREW';
		$expected_where = 'President = 1';

		$db_access = new DbAccessor();

		$actual = $db_access->db_query_update($expected_keys, $expected_values, $expected_table, $expected_where);
		$this->assertEquals("UPDATE CREW SET\n\tlastname = Beeblebrox,\n\tfirstname = Zaphod\nWHERE\n\t$expected_where",
				$actual);
	}

	function test_db_query_delete() {
		$expected_table = 'CREW';
		$expected_where = 'President = 1';
		
		$db_access = new DbAccessor();

		$actual = $db_access->db_query_delete($expected_table, $expected_where);
		$this->assertEquals("DELETE FROM $expected_table WHERE $expected_where", $actual);
	}

	function test_db_sqlize() {
		$db_access = new DbAccessor();

		$actual = $db_access->db_sqlize("   'some 'string   ");
		$this->assertEquals('"\\\'some \\\'string"', $actual);
	}

	function test_db_desqlize() {
		$db_access = new DbAccessor();

		$actual = $db_access->db_desqlize('\\\'some \\\'string');
		$this->assertEquals("'some 'string", $actual);
	}
}

?>
